package com.process.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.process.controller.util.Code;
import com.process.controller.util.R;
import com.process.dao.pojoDao.PracticalTaskDao;
import com.process.dao.publish.CourseTaskPublishDao;
import com.process.dao.publish.CourseTaskPublishVoDao;
import com.process.dao.publish.TaskBaseDao;
import com.process.dao.team.ProcessTaskDao;
import com.process.dao.team.ProcessTaskVoDao;
import com.process.dao.voDao.PracticalTaskVoDao;
import com.process.dao.voDao.TaskBaseVoDao;
import com.process.domain.publi.PracticalTask;
import com.process.domain.publi.PracticalTaskVo;
import com.process.domain.publish.CourseTaskPublish;
import com.process.domain.publish.CourseTaskPublishVo;
import com.process.domain.publish.TaskBase;
import com.process.domain.publish.TaskBaseVo;
import com.process.domain.team.ProcessTask;
import com.process.domain.team.ProcessTaskVo;
import com.process.service.publish.CourseTaskPublishService;
import com.process.service.score.ScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.List;

/**
 * @Description:
 * @ClassName: CourseController
 * @Author: 殷凡卓
 * @Date: 2023/5/1 10:18
 */

@RestController
@Slf4j
@RequestMapping("/inner")
public class CourseController {
    @Autowired
    private PracticalTaskVoDao practicalTaskVoDao;
    @Autowired
    private CourseTaskPublishService courseTaskPublishService;
    @Autowired
    private TaskBaseDao taskBaseDao;
    @Autowired
    private TaskBaseVoDao taskBaseVoDao;
    @Autowired
    private CourseTaskPublishVoDao courseTaskPublishVoDao;
    @Autowired
    private ProcessTaskDao processTaskDao;
    @Autowired
    private ProcessTaskVoDao processTaskVoDao;
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private PracticalTaskDao practicalTaskDao;
    @Autowired
    private CourseTaskPublishDao courseTaskPublishDao;

    @GetMapping("practicalTaskCourseVos")
    public R getpracticalTaskCourseVos(ServletRequest servletRequest) {

        String username = (String) servletRequest.getAttribute("username");
        Integer identity = (Integer) servletRequest.getAttribute("identity");
        List<PracticalTaskVo> practicalTaskVos = practicalTaskVoDao.selectVoByCourseAdviserPhone(username);

        return new R(Code.GET_OK, practicalTaskVos, "查询CourseVos");
    }

    @GetMapping("courseClass")
    public R getCourseClass(ServletRequest servletRequest) {

        String username = (String) servletRequest.getAttribute("username");
        Integer identity = (Integer) servletRequest.getAttribute("identity");
        List<PracticalTask> courseClass = practicalTaskVoDao.selectClassByCourseAdviserPhone(username);

        return new R(Code.GET_OK, courseClass, "查询course_Class");
    }

    /**
     * course_publish
     * 新增 —— courespublish中含有题号，相当于发布publish的时候发布多个题
     *
     * @param courseTaskPublishes
     * @param servletRequest
     * @return
     */
    @PostMapping("coursePublish")
    public R setCoursePublish(@RequestBody List<CourseTaskPublish> courseTaskPublishes, ServletRequest servletRequest) {
//       查询该课题是否已存在发布表
        CourseTaskPublish courseTaskPublish = courseTaskPublishes.get(0);
        List<CourseTaskPublish> courseTaskPublishes1 = courseTaskPublishDao.selectList(
                new LambdaQueryWrapper<CourseTaskPublish>()
                        .eq(CourseTaskPublish::getPracticalId, courseTaskPublish.getPracticalId()));

        if (courseTaskPublishes1.size()!=0) {

            return new R(Code.SAVE_ERR, courseTaskPublishes1, "该课程设计已发布！");
        } else {

            boolean saveBatch = courseTaskPublishService.saveBatch(courseTaskPublishes);
            return new R(Code.SAVE_OK, saveBatch, "新增课程设计发布表成功");
        }
    }

    /**
     * 新增题目
     * 实际新增的是coursePublish，coursePublish中有字段为题号
     *
     * @param courseTaskPublishes
     * @param servletRequest
     * @return
     */
    @PostMapping("addTaskbase")
    public R addTaskbase(@RequestBody List<CourseTaskPublish> courseTaskPublishes, ServletRequest servletRequest) {
        boolean saveBatch = courseTaskPublishService.saveBatch(courseTaskPublishes);

        return new R(Code.SAVE_OK, saveBatch, "新增题目");
    }

    @GetMapping("taskBase")
    public R getTaskBase(ServletRequest servletRequest) {
        List<TaskBase> taskBases = taskBaseDao.selectList(null);

        return new R(Code.GET_OK, taskBases, "查询题库");
    }

    @GetMapping("taskbaseVo/{practicalId}")
    public R getTaskbaseVo(@PathVariable long practicalId, ServletRequest servletRequest) {
        List<TaskBaseVo> taskBaseVos = taskBaseVoDao.selectByPracticalId(practicalId);

        return new R(Code.GET_OK, taskBaseVos, "select_course_taskbase");
    }


    @DeleteMapping("/allCourse/{practicalId}")
    public R deleteCoursePublish(@PathVariable long practicalId, ServletRequest servletRequest) {

        boolean remove = courseTaskPublishService.remove(new LambdaQueryWrapper<CourseTaskPublish>()
                .eq(CourseTaskPublish::getPracticalId, practicalId));

        return new R(Code.GET_OK, remove, "课程设计发布表删除成功");
    }


    @DeleteMapping("/course/{publishId}")
    public R deleteTaskBase(@PathVariable long publishId, ServletRequest servletRequest) {

        boolean remove = courseTaskPublishService.remove(new LambdaQueryWrapper<CourseTaskPublish>()
                .eq(CourseTaskPublish::getPublishId, publishId));

        return new R(Code.GET_OK, remove, "题目删除成功");
    }


    @GetMapping("MycoursePublish")
    public R getCoursePublishByTeacherPhone(ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");
        List<CourseTaskPublishVo> courseTaskPublishVos = courseTaskPublishVoDao.selectVoByteacherPhone(username);

        return new R(Code.GET_OK, courseTaskPublishVos, "查询courseClass");
    }

    @PostMapping("course/processTask")
    public R getprocessTask(@RequestBody ProcessTask processTask, ServletRequest servletRequest) {
        int insert = processTaskDao.insert(processTask);

        long processType = processTask.getProcessType();

        if (processType != 1) {

            return new R(Code.SAVE_OK, insert, "发布课程设计过程任务");
        } else {
            long practicalId = processTask.getPracticalId();
            PracticalTask practicalTask = practicalTaskDao.selectOne(new LambdaQueryWrapper<PracticalTask>()
                    .eq(PracticalTask::getPracticalId, practicalId));
            long classId = practicalTask.getClassId();
            scoreService.setMouses(classId, processTask.getProcessId());

            return new R(Code.SAVE_OK, insert, "发布课程设计最终任务");
        }


    }

    @GetMapping("myCourseProcessTask")
    public R getMyCourseProcessTask(ServletRequest servletRequest) {
        //获取我的电话号码
        String username = (String) servletRequest.getAttribute("username");
        List<ProcessTaskVo> processTaskVos = processTaskVoDao.selectMyCourseProcessTaskByTeacherPhone(username);
        return new R(Code.SAVE_OK, processTaskVos, "查看我的course过程任务");
    }


}
